package cn.edu.buaa.cnsatm.traffic_big_data_server.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author: LX
 * @className: DateUtil
 * @packageName:
 * @description: 对日期的操作
 * @data: 2019-10-12
 **/
public class DateUtil{

    public static Long getDateStringDifference(String dateStringX, String dateStringY) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: dateStringDifference
         * @description: 判断两个时间差多少秒(前者减后者)
         * @return: 以整数返回
         */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateX = formatter.parse(dateStringX);
        Date dateY = formatter.parse(dateStringY);
        return (dateX.getTime() - dateY.getTime())/ 1000;
    }
    public static int dateStringCompare(String dateStringX, String dateStringY) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: dateStringCompare
         * @description: 比较两个 yyyy-MM-dd HH:mm:ss 格式的String时间谁更晚
         * @return: 如果X比Y晚，返回1；如果X和Y一样，返回0；如果Y比X晚，返回-1
         */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(dateStringX.length() == 10){ formatter = new SimpleDateFormat("yyyy-MM-dd"); }
        Date dateX = formatter.parse(dateStringX);
        formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(dateStringY.length() == 10){ formatter = new SimpleDateFormat("yyyy-MM-dd"); }
        Date dateY = formatter.parse(dateStringY);
        if(dateX.getTime() >  dateY.getTime()){return  1;}
        if(dateX.getTime() == dateY.getTime()){return  0;}
        if(dateX.getTime() <  dateY.getTime()){return -1;}
        return Integer.parseInt(null);
    }
    public static void main(String args[]) throws ParseException {
        //System.out.println(turnDateTimeToSecond(getCurrentTime()));
        System.out.println(getDateTimeAfterSecond("2019-10-28 11:30:00", -60 * 15));
//        System.out.println(getRealTime("201910191111"));
//        String s = "2019-10-01 11:11:11";
//        s = "2019-10-01";
//        System.out.println(getDateSpecificTime(s, 0));
//        System.out.println(getDateSpecificTime(s, 1));
//        System.out.print(traversalMinuteInDay("2019-10-10 00:00:00"));
//        System.out.print(traversalDateBetweenTwoDate("2019-10-12","2019-11-02"));
    }
    public static long getHowMoneyTenMinute(String lowerBound, String upperBound) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: getDateSpecificTime
         * @description: 查询两个日期(YYYY-MM-DD HH:MM:SS之间有多少个10min片段)，用来除
         * @date: 2019/10/13
         */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date lowerBoundDate = formatter.parse(lowerBound);
        Date upperBoundDate = formatter.parse(upperBound);
        Long numTenMinute = (upperBoundDate.getTime() - lowerBoundDate.getTime()) / (10 * 60 * 1000);
        return numTenMinute;

    }
    public static String getCurrentTime(){
        /**
         * @author:  LX
         * @methodsName: getCurrentTime
         * @description: 获取当前时间精确到秒
         * @return
         * @date: 2019/10/19
         */
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curTime = dateFormat.format(date);//获取当前时间
        return curTime;
    }
    public static Long turnDateTimeToSecond(String time) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: getCurrentTime
         * @description: 获取当前时间精确到秒
         * @return
         * @date: 2019/10/24
         */
        String newTime = getDateSpecificTime(time, 0);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date specificDate = formatter.parse(newTime);
        return specificDate.getTime();
    }
    public static String getRealTime(String fakeTime) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: getRealTime
         * @description: 将YYYYMMDDHHMM变成YYYY-MM-DD HH-mm
         * @return
         * @date: 2019/10/19
         */
        String realTime = fakeTime.substring(0, 4) + "-" + fakeTime.substring(4, 6) + "-" + fakeTime.substring(6, 8);
        if(fakeTime.length() > 10){
            realTime = realTime + " " + fakeTime.substring(8, 10) + ":" + fakeTime.substring(10, 12);
        }
        return  getDateSpecificTime(realTime, 0);
    }
    public static String getDateSpecificTime(String rawDate, Integer boundType) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: getDateSpecificTime
         * @description: boundType0前边界，1是后边界，将YYYY-MM和YYYY-MM-DD都变为YYYY-MM-DD HH:MM:SS，如果有MM:SS则将SS清0，MM变成整10min
         * @return
         * @date: 2019/10/13
         */
        String specificDTime = null;
        /*补上-DD HH:MM:SS*/
        if(rawDate.length() <= 7){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date specificDate = formatter.parse(rawDate + "-01");
            Calendar specificCalendar = Calendar.getInstance();
            specificCalendar.setTime(specificDate);
            if(boundType == 0){
                //specificCalendar.add(Calendar.DATE, 1);
                specificDate = specificCalendar.getTime();
                specificDTime = formatter.format(specificDate);
                specificDTime = specificDTime + " 00:00:00";
            }
            else if(boundType == 1){
                //specificCalendar.set(Calendar.DAY_OF_MONTH, 1);
                specificCalendar.add(Calendar.MONTH, 1);
                specificDate = specificCalendar.getTime();
                specificDTime = formatter.format(specificDate);
                specificDTime = specificDTime + " 00:00:00";
            }
        }
        /*补上HH:MM:SS*/
        else if(rawDate.length() <= 10){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date specificDate = formatter.parse(rawDate);
            Calendar specificCalendar = Calendar.getInstance();
            specificCalendar.setTime(specificDate);
            //specificDate.setDate();
            if(boundType == 0){ specificDTime = rawDate + " 00:00:00";}
            else if(boundType == 1){
                specificCalendar.add(Calendar.DATE, 1);
                specificDate = specificCalendar.getTime();
                specificDTime = formatter.format(specificDate);
                specificDTime = specificDTime + " 00:00:00";
            }
        }
        else if(rawDate.length() <= 19){
            return rawDate;//带时分秒的已经够详细了，直接返回
            /*注释掉的部分为SS清空，MM变成10的整数倍，旧的方法有错误，这里不用了*/
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date specificDate = formatter.parse(rawDate);
//            Calendar specificCalendar = Calendar.getInstance();
//            specificCalendar.setTime(specificDate);
//            long deltTime = specificDate.getTime();
//            if(boundType == 0){
//                deltTime = (long) Math.ceil((double)deltTime / (60 * 10 * 1000));
//                deltTime = deltTime * 60 * 10 * 1000;
//                specificDate.setTime(deltTime);
//                specificDTime = formatter.format(specificDate);
//            }
//            else if(boundType == 1){
//                deltTime = deltTime / (60 * 10 * 1000);
//                deltTime = deltTime * 60 * 10 * 1000;
//                specificDate.setTime(deltTime);
//                specificDTime = formatter.format(specificDate);
//            }
        }

        return specificDTime;
    }
    public static String getDateTimeAfterSecond(String currentTime, int second) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: getDateTimeAfterSecond
         * @description: 将currentTime向后移动second秒
         * @param: 传入的currentTime为"yyyy-MM-dd HH:mm:ss"
         */
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(currentTime.length() == 10){formatter = new SimpleDateFormat("yyyy-MM-dd");}
        Date specificDate = formatter.parse(currentTime);
        Calendar specificCalendar = Calendar.getInstance();
        specificCalendar.setTime(specificDate);
        specificCalendar.add(Calendar.SECOND, second);
        String newTime = formatter.format(specificCalendar.getTime());
        return newTime;
    }
    public static List<String> traversalMinuteInDay(String dateString) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: traversalMinuteInDay
         * @description: 遍历一天内的所有长度为minute的时间片段
         * @param: 传入的dateString为yyyy-MM-dd 00:00:00格式
         * @return: 存放日期参数间的所有片段，以List<String>形式返回
         */
        List<String> minuteList = new ArrayList<String>();
        minuteList.add(dateString);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //SimpleDateFormat formatter_easy = new SimpleDateFormat("yyyy-MM-dd");

        Date minuteLowerBound = formatter.parse(dateString);
        Date minuteUpperBound = formatter.parse(dateString);
        Calendar minuteLowerBoundCalendar = Calendar.getInstance();
        Calendar minuteUpperBoundCalendar = Calendar.getInstance();
        minuteLowerBoundCalendar.setTime(minuteLowerBound);
        minuteUpperBoundCalendar.setTime(minuteLowerBound);

        minuteUpperBoundCalendar.add(Calendar.HOUR, 24);//加24个小时，获取当天结束的时间

        /*将Calendar转为字符串，便于存储，判等*/
        String minuteLowerBoundString = formatter.format(minuteLowerBoundCalendar.getTime());
        String minuteUpperBoundString = formatter.format(minuteUpperBoundCalendar.getTime());
        do{
            minuteLowerBoundCalendar.add(Calendar.MINUTE, 10);
            minuteLowerBoundString = formatter.format(minuteLowerBoundCalendar.getTime());
            minuteList.add(minuteLowerBoundString);
        }while(!(minuteLowerBoundString.equals(minuteUpperBoundString)));
        return minuteList;
    }

    public static List<String> traversalDateBetweenTwoDate(String dateLowerBound, String dateUpperBound) throws ParseException {
        /**
         * @author:  LX
         * @methodsName: traversalDateBetweenTwoDate
         * @description: 遍历两个日期间的所有日期（yyyymmdd）
         * @return: 存放日期参数间的所有日期，以List<String>形式返回
         */
        List<String> dateList = new ArrayList<String>();
        /*这里只需要年月日*/
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date dateLowerBoundDate = formatter.parse(dateLowerBound);
        Date dateUpperBoundDate = formatter.parse(dateUpperBound);
        /*使用日期可以一天一天增加的Calendar*/
        Calendar dateLowerBoundCalendar = Calendar.getInstance();
        Calendar dateUpperBoundCalendar = Calendar.getInstance();
        /*用Date对Calendar初始化*/
        dateLowerBoundCalendar.setTime(dateLowerBoundDate);
        dateUpperBoundCalendar.setTime(dateUpperBoundDate);
        /*将Calendar转为字符串，便于存储，判等*/
        String dateLowerBoundString = formatter.format(dateLowerBoundCalendar.getTime());
        String dateUpperBoundString = formatter.format(dateUpperBoundCalendar.getTime());
        while(!(dateLowerBoundString.equals(dateUpperBoundString))){
            /*数据表名没有-，所以存的时候给去掉*/
            dateList.add(dateLowerBoundString.replaceAll("-",""));
            dateLowerBoundCalendar.add(Calendar.DATE, 1);
            dateLowerBoundString = formatter.format(dateLowerBoundCalendar.getTime());
        }
        return dateList;
    }
}